import {
  wxLoginCustom
} from "../../utils/util";

//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    theme: 'light',
    url: '',
    name: '大西瓜瓜瓜',
    list: [{
        id: 'examples',
        name: '精选案例',
        open: true,
        pages: [{
          name: '我的收藏',
          url: 'detail_three',
          data: 'collection'
        }]
      },
      {
        id: 'go',
        name: '走进永昌',
        open: true,
        pages: [{
          name: '联系我们',
          url: 'detail_contact_us'
        }]
      },
      {
        id: 'ask',
        name: '装修咨询',
        open: true,
        pages: [{
          name: '我的咨询',
          url: 'detail_three',
          data: 'ask'
        }]
      }
    ]
  },
  onLoad: async function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 获取
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })

      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  /**
   * 点击列表，张开或者关闭。
   */
  kindToggle: function (e) {
    var id = e.currentTarget.id,
      list = this.data.list;
    for (var i = 0, len = list.length; i < len; ++i) {
      if (list[i].id == id) {
        list[i].open = !list[i].open
      } else {
        // list[i].open = false
      }
    }
    this.setData({
      list
    });
  },
  /**
   * 点击头部
   */
  clickHead: function (e) {
    wx.navigateTo({
      url: "/pages/center_edit/center_edit",
    })
  },
  /**
   * 并且收藏用户信息
   */
  getUserInfoCollection: function (e) {
    const {
      userInfo
    } = e.detail
    app.globalData.userInfo = userInfo
    this.setData({
      hasUserInfo: true,
      userInfo
    })
  },
  /**
   * 退出登录
   */
  logout: function (e) {
    app.globalData.userInfo = null
    this.setData({
      hasUserInfo: false,
      userInfo: null
    })
  }
})